/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bingkaimedia.merchantbeta.app;

import java.util.ArrayList;
import java.util.HashMap;

import com.bingkaimedia.merchantbeta.R;
import com.bingkaimedia.merchantbeta.libs.LazyAdapter;
import com.bingkaimedia.merchantbeta.libs.SessionManager;
import com.bingkaimedia.merchantbeta.libs_slidemenu.BaseActivity;
import com.bingkaimedia.merchantbeta.libs_tab.TabsPagerAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.jeremyfeinstein.slidingmenu.lib.PagerSlidingTabStrip;
//import com.astuetz.viewpager.extensions.sample.TabsPagerAdapter;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;


@SuppressLint("NewApi")
public class Home extends BaseActivity {
	
	

	// tab
	private final Handler handler = new Handler();
	private PagerSlidingTabStrip tabs;
	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	private MyPagerAdapter adapter1;
	private Drawable oldBackground = null;
	private int currentColor = 0xFF2490f4;
	private final String[] TITLES = { "All", "Popular", "Recent"};
 
	

	// XML node keys 
	public static final String KEY_VOUCHER_ID = "voucher_id";
	public static final String KEY_VOUCHER_NAME = "voucher_name"; 
	public static final String KEY_VOUCHER_TOTAL = "voucher_total";
	public static final String KEY_VOUCHER_TERSEDIA = "voucher_tersedia";
	public static final String KEY_MERCHANT_NAME = "merchant_name";
	public static final String KEY_MERCHANT_CATEGORY = "merchant_category";
	public static final String KEY_MERCHANT_PHOTO = "merchant_photo";
	public static final String KEY_VOUCHER_DETAIL = "voucher_detail";
	public static final String KEY_VOUCHER_DESCRIPTION = "voucher_description";
	public static final String KEY_MERCHANT_ID = "merchant_id";  
	public static final String KEY_MY_CHANNEL_ID = "my_channel_id";
	public static final String KEY_CHANNEL_ID = "channel_id";
	public static final String KEY_USERS_ID = "users_id";
	public static final String KEY_CHANNEL_NAME = "channel_name";
	public static final String KEY_CHANNEL_PHOTO = "channel_photo";
	 
	private String xml_voucher_id;
	private String xml_voucher_name;
	private String xml_voucher_total;
	private String xml_voucher_tersedia;
	private String xml_voucher_description;
	private String xml_voucher_detail;
	private String xml_merchant_id;
	private String xml_merchant_name;
	private String xml_merchant_category;
	private String xml_merchant_photo; 
	private String xml_my_channel_id;
	private String xml_channel_id;
	private String xml_users_id;
	private String xml_channel_name;
	private String xml_channel_photo; 
	private String xml_my_channel_id2;
	private String xml_channel_id2;
	private String xml_users_id2;
	private String xml_channel_name2;
	private String xml_channel_photo2;
	
	private static String session_userid;
	private static String session_username;
	private static String session_password;
	private static String session_email;
	private static String session_city;
	private static String session_checked;
	private static String session_channel;
	private static String session_category;
	
    
 
	// Session Manager Class
	SessionManager session;
	
	
	//adapter
	private LazyAdapter adapter;
	ArrayList<HashMap<String, String>> menuItems;
	ProgressDialog pDialog1, pDialog2;
	HashMap<String, String> map = new HashMap<String, String>();
	ArrayList<HashMap<String, String>> diskonba_list = new ArrayList<HashMap<String, String>>();
	int current_page = 0;
	public ListView lv;
	public GridView gridView1, gridView2;
	private SlidingMenu sm;
	
	private AdView adView;  
	
	/* Your ad unit id. Replace with your actual ad unit id. */  
	private static final String AD_UNIT_ID = "ca-app-pub-5739313827001128/3407071296";  
	
	
	
	public Home() {
		super(R.string.baru); 
	}


	@Override
	public void onBackPressed() { 
		Home.this.finish();
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }
	
	@Override  
	public void onResume() {    
		super.onResume();    
		if (adView != null) {      
			adView.resume();    
			}  
		}  
	
	
	@Override  
	public void onPause() {    
		if (adView != null) {     
			adView.pause();    
			}    
		super.onPause();  
		} 
	
	
	/** Called before the activity is destroyed. */  
	@Override  public void onDestroy() {    
		// Destroy the AdView.    
		if (adView != null) {      
			adView.destroy();    
			}    
		super.onDestroy();  
		}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		setContentView(R.layout.home); 
		
		
		// Create an ad.    
		adView = new AdView(this);    
		adView.setAdSize(AdSize.BANNER);    
		adView.setAdUnitId(AD_UNIT_ID);   
		
		// Add the AdView to the view hierarchy. The view will have no size    
		// until the ad is loaded.    
		LinearLayout layout = (LinearLayout) findViewById(R.id.ads);    
		layout.addView(adView);    
		
		// Create an ad request. Check logcat output for the hashed device ID to    
		// get test ads on a physical device.    
		AdRequest adRequest = new AdRequest.Builder()        
		.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        
		//.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")        
		.build();    
		
		// Start loading the ad in the background.    
		adView.loadAd(adRequest);  
		 
	
	
		fungsi_menu();
		
		//tab
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		viewPager = (ViewPager) findViewById(R.id.pager);
		mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
		adapter1 = new MyPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(mAdapter);
		
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
		viewPager.setPageMargin(pageMargin); 
		tabs.setDividerColor(currentColor);
		tabs.setIndicatorColor(currentColor);
		tabs.setUnderlineColor (currentColor); 
		tabs.setViewPager(viewPager);
		 

	    // session
		session = new SessionManager(getApplicationContext());
	    HashMap<String, String> user = session.getUserDetails();
	    session_userid = user.get(SessionManager.id_users);
	    session_username = user.get(SessionManager.username);
	    session_password = user.get(SessionManager.password);
	    session_email = user.get(SessionManager.email);
	    session_city = user.get(SessionManager.city);
	    session_checked = user.get(SessionManager.checked);
	    session_channel = user.get(SessionManager.channel);
	    session_category = user.get(SessionManager.category);
		
	      
	}
 
 
	public void fungsi_menu(){
		
		setSlidingActionBarEnabled(true);
		 
        sm = getSlidingMenu();
		 
		LinearLayout header_vertical_divider_1 = (LinearLayout) findViewById(R.id.header_vertical_divider_1);
		LinearLayout header_vertical_divider_2 = (LinearLayout) findViewById(R.id.header_vertical_divider_2);
		ImageView diskonion_iconmenu = (ImageView) findViewById(R.id.diskonion_iconmenu);
		ImageView diskonion_iconhome = (ImageView) findViewById(R.id.diskonion_iconhome);
		ImageView diskonion_iconsearch = (ImageView) findViewById(R.id.diskonion_iconsearch);
		ImageView diskonion_iconcategory = (ImageView) findViewById(R.id.diskonion_iconcategory);
		
		 
		
		diskonion_iconmenu.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				sm.showMenu(); 
			}
		}); 
		
		diskonion_iconhome.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				//sm.showSecondaryMenu();
 				Intent intent=new Intent(getApplicationContext(),Home.class);
 				startActivity(intent);
			}
		});
		
		diskonion_iconsearch.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				//sm.showSecondaryMenu();
 				Intent intent=new Intent(getApplicationContext(),Search.class);
 				startActivity(intent);
			}
		});
		
		diskonion_iconcategory.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				sm.showSecondaryMenu(); 
			}
		});
 		
	}
	
	public class MyPagerAdapter extends FragmentPagerAdapter {

		private final String[] TITLES =  { "All", "Popular", "Recent"};

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		@Override
		public int getCount() {
			return TITLES.length;
		}

		@Override
		public Fragment getItem(int position) {
			viewPager.setCurrentItem(position);
			//return SuperAwesomeCardFragment.newInstance(position);
			return null;
		}

	}
	
	
	

}